package com.example.keyboardedittext;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import com.example.keyboardedittext.edittext.LayoutEditText;
import com.example.keyboardedittext.fraction.FractionCallBack;
import com.example.keyboardedittext.fraction.FractionLayoutView;
import com.example.keyboardedittext.keyboard.KeyBoardCallBack;
import com.example.keyboardedittext.keyboard.KeyBoardView;


public class MainActivity extends Activity {

    private LayoutEditText layoutEditText;
    private KeyBoardView keyBoardView;
    private FractionLayoutView layoutFraction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        layoutEditText.signEditText(10);
    }

    private void initView() {
        layoutEditText = (LayoutEditText)findViewById(R.id.layout_edittext);
        layoutFraction = (FractionLayoutView)findViewById(R.id.layout_fraction);
        keyBoardView = (KeyBoardView)findViewById(R.id.keyboard_view);
        keyBoardView.setOnClickKeyFinishListener(new KeyBoardCallBack() {
            @Override
            public void getFinishEditValue(String text) {
                if(layoutFraction.getVisibility()==View.VISIBLE){
                    layoutFraction.getCurrentEditText().getText().append(text);
                }else{
                    layoutEditText.getCurrentEditText().getText().append(text);
                }

            }

            @Override
            public void deleteKeyBoard() {
                if(layoutFraction.getVisibility()==View.VISIBLE){
                    if(layoutFraction.getCurrentEditText().getText().length() !=0){
                        layoutFraction.getCurrentEditText().getText().delete(layoutFraction.getCurrentEditText().getText().length() - 1, layoutFraction.getCurrentEditText().getText().length());
                    }
                }else{
                    if (layoutEditText.getCurrentEditText().getText().length() != 0) {
                        layoutEditText.getCurrentEditText().getText().delete(layoutEditText.getCurrentEditText().getText().length() - 1, layoutEditText.getCurrentEditText().getText().length());
                    }

                }
                layoutEditText.deleteEditTextSpan();

            }

            @Override
            public void deleteLongKeyBoard() {
                if(layoutFraction.getVisibility()==View.VISIBLE){
                    int currentSelect = layoutFraction.getCurrentEditText().getSelectionStart();
                    if (layoutFraction.getCurrentEditText().getText().length() != 0) {
                        layoutFraction.getCurrentEditText().getText().delete(0, currentSelect);
                        layoutFraction.getCurrentEditText().getText().clear();
                    }
                }else {
                    int currentSelect = layoutEditText.getCurrentEditText().getSelectionStart();
                    if (layoutEditText.getCurrentEditText().getText().length() != 0) {
                        layoutEditText.getCurrentEditText().getText().delete(0, currentSelect);
                        layoutEditText.getCurrentEditText().getText().clear();
                    }
                }
            }

            @Override
            public void onNext() {
                layoutEditText.setNextEditText();

            }

            @Override
            public void onLast() {
                layoutEditText.setLastEditText();
            }

            @Override
            public void closeKeyBoard() {
                keyBoardView.setVisibility(View.GONE);
            }

            @Override
            public void showMolecular() {
                if(layoutFraction.getVisibility()==View.GONE){
                    layoutFraction.setVisibility(View.VISIBLE);
                }else{
                    layoutFraction.setVisibility(View.GONE);
                }
            }
        });

        layoutEditText.setOnItemClick(new LayoutEditText.OnItemEditClick() {
            @Override
            public void onItemClick(int currentClick) {
                if (keyBoardView.getVisibility() == View.GONE) {
                    keyBoardView.setVisibility(View.VISIBLE);
                }
            }
        });
        layoutFraction.setFractionCallBack(new FractionCallBack() {
            @Override
            public void getFractionImg(Bitmap bitmap) {
                layoutEditText.showFraction(bitmap);
            }
        });
    }

}
